package com.TeaManage.servlet.Warehouse;

import com.TeaManage.bean.Inventory;
import com.TeaManage.bean.Tea;
import com.TeaManage.util.GokJDBCUtil;

import java.io.PrintWriter;
import java.util.List;

@javax.servlet.annotation.WebServlet(name = "WarehouseDetailServlet", urlPatterns = "/WarehouseDetailServlet")
public class WarehouseDetailServlet extends javax.servlet.http.HttpServlet {
    @Override
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        String method = request.getParameter("method");
        List<Inventory> inventoryList=null;
        if("search".equals(method)) {
            String detailname = request.getParameter("Detailname");
            if (detailname == null) detailname = "";
            // 使用SQL JOIN查询，添加通配符实现模糊搜索
            String sql = "SELECT ti.* FROM tea_inventory ti JOIN tea t ON ti.tea_id = t.tea_id WHERE t.tea_name LIKE ?";
            inventoryList = GokJDBCUtil.queryAll(sql, Inventory.class, "%" + detailname + "%");
            String teaSql="select * from tea";
            List<Tea> teas = GokJDBCUtil.queryAll(teaSql, Tea.class);
//            request.setAttribute("inventoryList",inventoryList);
            request.setAttribute("teas",teas);
//            request.setAttribute("inventoryList", inventoryList);
//            request.getRequestDispatcher("/views/warehouse/warehouseDetail.jsp").forward(request,response);
        }else {
            String id = request.getParameter("id");
            String sql="select * from tea_inventory where warehouse_id=?";
            inventoryList = GokJDBCUtil.queryAll(sql, Inventory.class, id);
            String teaSql="select * from tea";
            List<Tea> teas = GokJDBCUtil.queryAll(teaSql, Tea.class);
//            request.setAttribute("inventoryList",inventoryList);
            request.setAttribute("teas",teas);
//            request.getRequestDispatcher("/views/warehouse/warehouseDetail.jsp").forward(request,response);
        }
        request.setAttribute("inventoryList",inventoryList);
        request.getRequestDispatcher("/views/warehouse/warehouseDetail.jsp").forward(request,response);
    }

    @Override
    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        String id = request.getParameter("id");
        String sql="select * from tea_inventory where warehouse_id=?";
        List<Inventory> inventoryList = GokJDBCUtil.queryAll(sql, Inventory.class, id);
        String teaSql="select * from tea";
        List<Tea> teas = GokJDBCUtil.queryAll(teaSql, Tea.class);
        request.setAttribute("inventoryList",inventoryList);
        request.setAttribute("teas",teas);
        request.getRequestDispatcher("/views/warehouse/warehouseDetail.jsp").forward(request,response);

    }
}
